package com.lychee.socketclient;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class MainActivity extends AppCompatActivity {
    private TextView mTvSend, mTvResult;
    private EditText mEtInput;


    public Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                mTvResult.setText("server:" + msg.obj + "\n");
            }
        }

    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTvSend = findViewById(R.id.mTvSend);
        mTvResult = findViewById(R.id.mTvResult);
        mEtInput = findViewById(R.id.mEtInput);


        mTvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputContent = mEtInput.getText().toString();
                mTvResult.setText("client:" + inputContent + "\n");
                //启动线程 向服务器发送和接收信息
                new Thread(new SendData(inputContent), "客户端数据线程").start();
                mEtInput.setText("");
            }
        });
    }

    private class SendData implements Runnable {

        private String sendData;

        public SendData(String sendData) {
            this.sendData = sendData;
        }

        @Override
        public void run() {

            //定义消息
            Message msg = new Message();
            msg.what = 1;

            try {
                //连接服务器 并设置连接超时为5秒
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress("192.168.1.115", 30000), 1000);

                //获取输入输出流
                OutputStream ou = socket.getOutputStream();
                //获取输出输出流
                BufferedReader bff = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                //向服务器发送信息
                ou.write(sendData.getBytes("utf-8"));
                ou.flush();

                //读取发来服务器信息
                String result = "";
                String buffer = "";
                while ((buffer = bff.readLine()) != null) {
                    result = result + buffer;
                }
                msg.obj = result.toString();
                //发送消息 修改UI线程中的组件
                myHandler.sendMessage(msg);
                //关闭各种输入输出流
                bff.close();
                ou.close();
                socket.close();
            } catch (SocketTimeoutException aa) {
                //连接超时 在UI界面显示消息
                msg.obj = "服务器连接失败！请检查网络是否打开";
                //发送消息 修改UI线程中的组件
                myHandler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
